package com.huitone.gddw.controller.business;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huitone.gddw.controller.common.CommonController;
import com.huitone.gddw.dao.entity.SmLog;
import com.huitone.gddw.dao.entity.SmModuleVar;
import com.huitone.gddw.model.support.Message;
import com.huitone.gddw.service.ISmModuleVarService;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 短信模板变量控制器
 * @author Weiliyu
 *
 */

@Controller
@RequestMapping(value="smModuleVar")
public class SmModuleVarController extends CommonController {
	
	private final static Logger LOGGER = Logger.getLogger(SmModuleVarController.class);
	
	@Autowired
	private ISmModuleVarService smModuleVarService;
	
	/**
	 * 获取所有有效的变量
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getAllVar")
	public List<SmModuleVar> getAllVar(){
		try {
			List<SmModuleVar> list = smModuleVarService.selectAllVar();
			return list;
		} catch (Exception e) {
			LOGGER.error("获取所有的短信模板变量过程中出现异常：" + e.getMessage(), e);
			return new ArrayList<SmModuleVar>();
		}
	}
	
	/**
	 * 根据业务端获取变量
	 * @param svrNode
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getVarBySvr")
	public List<SmModuleVar> getVarBySvr(String svrNode){
		try {
			List<SmModuleVar> list = smModuleVarService.selecVartBySvrNode(svrNode);
			if (list==null || list.size()==0) {
				list = new ArrayList<SmModuleVar>();
			}
			return list;
		} catch (Exception e) {
			LOGGER.error("根据业务端获取短信模板变量过程中出现异常：" + e.getMessage(), e);
			return new ArrayList<SmModuleVar>();
		}
	}
	
	@ResponseBody
	@RequestMapping("getVarBySvrAndCom")
	public List<SmModuleVar> getVarBySvrAndCom(String svrNode){
		try {
			List<SmModuleVar> list = smModuleVarService.selecVarBySvrAndCom(svrNode);
			if (list==null || list.size()==0) {
				list = new ArrayList<SmModuleVar>();
			}
			return list;
		} catch (Exception e) {
			LOGGER.error("根据业务端获取短信模板变量过程中出现异常：" + e.getMessage(), e);
			return new ArrayList<SmModuleVar>();
		}
	}
	
	@ResponseBody
	@RequestMapping("getVarBySet")
	public List<SmModuleVar> getVarBySet(String svrNode){
		try {
			 List<SmModuleVar> list = smModuleVarService.selecVarBySet(svrNode);
			 if (list==null || list.size()==0) {
					list = new ArrayList<SmModuleVar>();
				}
			return list;
		} catch (Exception e) {
			LOGGER.error("根据业务端获取短信模板变量过程中出现异常：" + e.getMessage(), e);
			return new ArrayList<>();
		}
	}
	
	/**
	 * 新增变量
	 * @param entity
	 * @return
	 */
	@ResponseBody
	@RequestMapping("save")
	public Message save(SmModuleVar entity){
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]添加短信模板变量";
		boolean isSuccess = true;
		String msg;
		try {
			//同一业务端下变量名不重复
			if(entity.getSvrNode().equals("")){
				return 	new Message(false, "请选择业务端！");
			}
			if(entity.getMoVar().equals("")){
				return 	new Message(false, "请填写变量名称！");
			}
			List<SmModuleVar> list = smModuleVarService.selecVartByMoVar(entity);
			if (list.size()>0) {
				return new Message(false, "此业务端下该变量名称或变量ID已存在！");
			}	
			long l = System.currentTimeMillis();
			entity.setMoContent("["+String.valueOf(l)+"]");
			smModuleVarService.save(entity);
			msg = "保存成功！";
		} catch (Exception e) {
			LOGGER.error("保存短信模板变量过程中出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
			isSuccess = false;
			msg = "保存失败！";
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return new Message(isSuccess, msg);
	}
	
	/**
	 * 删除变量
	 * @param recId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="delete")
	public Message delete(String recId) {
		super.logType = SmLog.LOGTYPE_DELETE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]删除短信模板变量";
		boolean isSuccess = true;
		String msg;
		try {
			smModuleVarService.delete(recId);
			msg = "删除成功！";
		} catch (Exception e) {
			LOGGER.error("删除短信模板变量过程中出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
			isSuccess = false;
			msg = "删除失败！";
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return new Message(isSuccess, msg);
	}
	
	/**
	 * 获取修改数据 
	 * @param recId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="toUpdate")
	public SmModuleVar toUpdate(String recId) {
		try {
			SmModuleVar smModuleVar = smModuleVarService.selectByPrimaryKey(recId);
			return smModuleVar;
		} catch (Exception e) {
			LOGGER.error("跳转修改页面过程中出现异常：" + e.getMessage(), e);
			return new SmModuleVar();
		}
	}
	
	/**
	 * 修改变量信息
	 * @param entity
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="update")
	public Message update(SmModuleVar entity) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]修改短信模板变量";
		boolean isSuccess = true;
		String msg;
		try {
			//同一业务端下变量名不重复
			if(entity.getSvrNode().equals("")){
				return 	new Message(false, "请选择业务端！");
			}
			if(entity.getMoVar().equals("")){
				return 	new Message(false, "请填写变量名称！");
			}
			List<SmModuleVar> list = smModuleVarService.selecVartByMoVar(entity);
			if (list.size()>0) {
				return new Message(false, "此业务端下该变量已存在！");
			}
			
			SmModuleVar smModuleVar = smModuleVarService.selectByPrimaryKey(entity.getRecId());
			smModuleVar.setMoContent(entity.getMoContent());
			smModuleVar.setMoVar(entity.getMoVar());
			smModuleVar.setRemark(entity.getRemark());
			smModuleVar.setSvrNode(entity.getSvrNode());
			smModuleVarService.update(smModuleVar);
			msg = "修改成功！";
		} catch (Exception e) {
			LOGGER.error("修改短信模板变量过程中出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
			isSuccess = false;
			msg = "修改失败！";
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return new Message(isSuccess, msg);
	}

}
